package com.example.mvt;



import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class AdminActivity extends Activity {
	
	String username="";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin);
		
		
		ImageButton ib = (ImageButton)findViewById(R.id.edit);
	    ib.setOnClickListener(new View.OnClickListener() { 
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(AdminActivity.this, EditActivity.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib2 = (ImageButton)findViewById(R.id.delete); 
	    ib2.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(AdminActivity.this, DeleteActivity.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib3 = (ImageButton)findViewById(R.id.win1);  
	    ib3.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(AdminActivity.this, Winners.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib4 = (ImageButton)findViewById(R.id.votes);
	    ib4.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(AdminActivity.this, Votes.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		
		new getLoginInfo().execute();
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
	    // Handle item selection
	    switch (item.getItemId()) {	
	        case R.id.action_logout:
	        	new Logout().execute();
	        	Intent myIntent3 = new Intent(AdminActivity.this, LoginActivity.class);
				startActivityForResult(myIntent3, 0);
				AdminActivity.this.finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin, menu);
		return true;
	}
	
	//Get Login Info
	private class Logout extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... nothing) {
			
			MVTDataSource mds2 = new MVTDataSource(AdminActivity.this);
			
			mds2.open();
			
			try {
				mds2.logoutUser();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			mds2.close();
			
			return "";
		}
		
		@Override
		protected void onPostExecute(String whatever) {
				Intent myIntent = new Intent(AdminActivity.this, LoginActivity.class);
				startActivityForResult(myIntent, 0);
				AdminActivity.this.finish();
		}
		
	}
	
	//Get Login Info
		private class getLoginInfo extends AsyncTask<Void, Void, String>{
			@Override
			protected String doInBackground(Void... nothing) {
				
				MVTDataSource mds2 = new MVTDataSource(AdminActivity.this);
				
				mds2.open();
				
				try {
					username = mds2.getUsername();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				mds2.close();
				
				return username;
			}
			
			@Override
			protected void onPostExecute(String whatever) {
				if (username.equals("user")){
					Intent myIntent = new Intent(AdminActivity.this, LoginActivity.class);
					startActivityForResult(myIntent, 0);
					return;
				}
				else{
					return;
				}
			}
			
		}

}
